/*
 * Copyright (c) 2011 Junichiro Okuno
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 *
 * The views and conclusions contained in the software and documentation are those of the
 * authors and should not be interpreted as representing official policies, either expressed
 * or implied, of Junichiro Okuno.
 */

package tcp_net.management;

import tcp_net.management.debug.DebugInfo;
import java.io.File;

/**
	This class provides to manage files and directories on a secondary storage
	@author J. Okuno
*/
public class FileManager
{
	/**
		Debug information
	*/
	private final static boolean DEBUG = DebugInfo.FILE_MANAGER_DEBUG;
	
	/**
		A file object
	*/
	private File dir;
	
	/**
		Init the file manager
	*/
	public FileManager() 
	{
		DebugInfo.print(DEBUG, "FileManager created");
	}
	
	/**
		This method provides to create a directory
		@param path a file path
		@param dir_name a directory name
		@return true if a directory is created
	*/
	public boolean createDir(String path, String dir_name)
	{
		dir = new File(path+"/"+dir_name);
		if(!dir.exists()) 
		{ 
			DebugInfo.print(DEBUG, "Directory path: "+path+" name: "+dir_name+" created.");
			return dir.mkdir(); 
		}
		
		else 
		{ 
			DebugInfo.print(DEBUG, "Directory path: "+path+" name: "+dir_name+" NOT created.");
			return false; 
		}
	}
	
	/**
		This method provides to remove a directory
		@param path a file path
		@param dir_name a directory name
		@return true if a directory is removed
	*/
	public boolean deleteDir(String path, String dir_name)
	{
		dir = new File(path+"/"+dir_name);
		if(dir.exists()) 
		{ 
			DebugInfo.print(DEBUG, "Directory path: "+path+" name: "+dir_name+" deleted.");
			return dir.delete(); 
		}
		
		else 
		{ 
			DebugInfo.print(DEBUG, "Directory path: "+path+" name: "+dir_name+" NOT deleted.");
			return false; 
		}
	}
	
	/**
		This method provides to rename a directory
		@param path a file path
		@param old_dir_name a old directory name
		@param new_dir_name a new directory name
		@return true if a directory is renamed
	*/
	public boolean renameDir(String path, String old_dir_name, String new_dir_name)
	{
		dir = new File(path+"/"+old_dir_name);
		File dir2 = new File(path+"/"+new_dir_name);
		
		if(dir.exists() && !dir2.exists())
		{ 
			DebugInfo.print(DEBUG, "Directory path: "+path+" old name: "+old_dir_name+" new name: "+new_dir_name+" renamed.");
			return dir.renameTo(dir2); 
		}
		
		else 
		{ 
			DebugInfo.print(DEBUG, "Directory path: "+path+" old name: "+old_dir_name+" new name: "+new_dir_name+" NOT renamed.");
			return false; 
		}
	}
	
	/**
		This method provides to remove a file
		@param path a file path
		@param file_name a file name
		@return true if a file is removed
	*/
	public boolean deleteFile(String path, String file_name)
	{
		DebugInfo.print(DEBUG, "deletedFile() created.");
		return deleteDir(path, file_name);
	}
}